package com.example.UI;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.UI.database.MySQLDataHelper;
import com.example.ui.R;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private MySQLDataHelper mySQLDataHelper;
    private EditText username;
    private EditText password;
    private Button login;
    private Button register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mySQLDataHelper = new MySQLDataHelper(this);
        find();

        // 初始化EditText的背景
        username.setBackgroundResource(R.drawable.et_underline_unselected);
        password.setBackgroundResource(R.drawable.et_underline_unselected);

        // 设置焦点监听事件
        username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Log.e(TAG, "EditText1获得焦点");
                    username.setBackgroundResource(R.drawable.et_underline_selected);
                } else {
                    Log.e(TAG, "EditText1失去焦点");
                    username.setBackgroundResource(R.drawable.et_underline_unselected);
                }
            }
        });

        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Log.e(TAG, "EditText2获得焦点");
                    password.setBackgroundResource(R.drawable.et_underline_selected);
                } else {
                    Log.e(TAG, "EditText2失去焦点");
                    password.setBackgroundResource(R.drawable.et_underline_unselected);
                }
            }
        });
    }

    private void find(){
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login = findViewById(R.id.btn_login);
        register = findViewById(R.id.btn_register);

        login.setOnClickListener(this);
        register.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_login) {
            // 已有的登录逻辑代码
            String studentIdText = username.getText().toString().trim();
            String passwordText = password.getText().toString().trim();
            if (studentIdText.isEmpty() || passwordText.isEmpty()) {
                Toast.makeText(this, "学号或密码不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            boolean loginSuccess = mySQLDataHelper.login(studentIdText, passwordText);
            if (loginSuccess) {
                Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
                // 将学号存储在SharedPreferences中
                SharedPreferences sharedPreferences = getSharedPreferences("user_session", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("studentId", studentIdText);
                editor.apply();
                // 跳转到MainActivity
                Intent intent = new Intent(LoginActivity.this, StudentActivity.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "登录失败，学号或密码错误！", Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.btn_register) {
            // 跳转到注册界面的逻辑
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        } else {
            // 对于未知的view ID，可以在这里进行处理或者忽略
        }
    }

}